import { getPpbxToken, findUserByNumber } from "@/api/modules/index";
import { ElMessage } from "element-plus";
/**
 * 参数说明：
 *  webSocketURL：String    webSocket服务地址    eg: ws://127.0.0.1:8088/websocket (后端接口若为restful风格可以带参数)
 *  callback：为带一个参数的回调函数
 *  message：String 要传递的参数值（不是一个必要的参数）
 */
export default {
  webSocket: null as WebSocket | null,
  cishu: 0,
  url: "",
  // 初始化webSocket
  webSocketInit(webSocketURL: string): void {
    this.url = webSocketURL;
    this.webSocket = new WebSocket(webSocketURL);
    this.webSocket.onopen = this.onOpenCallback.bind(this);
    this.webSocket.onmessage = this.onMessageCallback.bind(this);
    this.webSocket.onerror = this.onErrorCallback.bind(this);
    this.webSocket.onclose = this.onCloseCallback.bind(this);
  },
  // 自定义回调函数
  setOpenCallback(callback: (event: Event) => void): void {
    if (this.webSocket) {
      this.webSocket.onopen = callback;
    }
  },
  setMessageCallback(callback: (event: MessageEvent) => void): void {
    if (this.webSocket) {
      this.webSocket.onmessage = callback;
    }
  },
  setErrorCallback(callback: (event: Event) => void): void {
    if (this.webSocket) {
      this.webSocket.onerror = callback;
    }
  },
  setCloseCallback(callback: (event: CloseEvent) => void): void {
    if (this.webSocket) {
      this.webSocket.onclose = callback;
    }
  },
  close(): void {
    if (this.webSocket) {
      this.webSocket.close();
    }
  },
  sendMessage(message: string): void {
    if (this.webSocket) {
      this.webSocket.send(message);
    }
  },
  // 回调函数示例
  onOpenCallback(event: Event): void {
    this.cishu = 0;
    console.log("WebSocket连接已打开");
  },
  onMessageCallback(event: MessageEvent): void {
    console.log("收到消息：", event.data);
  },
  onErrorCallback(event: Event): void {
    // let  token=""
    // let ipAndPort=""
    // getPpbxToken().then(res=>{
    // 	if(res!=null && res!=undefined){
    // 		let jsondata = JSON.parse(res.toString())
    // 		token=jsondata.token;
    // 		ipAndPort=jsondata.ipAndPort;
    // 		console.log(token)
    // 		console.log(ipAndPort)
    // 		this.url="wss://"+ipAndPort+"/openapi/v1.0/subscribe?access_token="+token
    // 	}
    // })
    // this.webSocketInit(this.url)
    // //this.webSocket = new WebSocket(this.url);
    //  console.error("WebSocket连接出错");
  },
  onCloseCallback(event: CloseEvent): void {
    let token = "";
    let ipAndPort = "";
    getPpbxToken().then(res => {
      if (res != null && res != undefined) {
        let jsondata = JSON.parse(res.toString());
        token = jsondata.token;
        ipAndPort = jsondata.ipAndPort;
        console.log(token);
        console.log(ipAndPort);
        this.url = "wss://" + ipAndPort + "/openapi/v1.0/subscribe?access_token=" + token;
      }
    });
    if (this.cishu > 5) {
      ElMessage.error("来电弹屏监听连续关闭次数大于" + this.cishu + "次，请检查ppbx连接");
      console.log("WebSocket连接已关闭次数大于" + this.cishu + "次，循环停止");
    } else {
      // 创建定时器
      const timerId = setTimeout(() => {
        console.log("定时器执行完毕");
        this.webSocketInit(this.url);
        // 定时器任务完成后，销毁定时器
        clearTimeout(timerId);
        this.cishu++;
      }, 3000);
    }

    //this.webSocket = new WebSocket(this.url);
    console.log("WebSocket连接已关闭");
  }
};
